<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<link href="${css}/bootstrap.min.css" rel="stylesheet"/>
<link href="${css}/sb-admin-2.css" rel="stylesheet"/>
<link href="${css}/patch.css" rel="stylesheet"/>
<link rel="stylesheet" href="${assets}/plugins/ztree/zTreeStyle/metro.css">
<script type="text/javascript" src="${js}/jquery.min.js"></script>
<script type="text/javascript" src="${assets}/plugins/ztree/jquery.ztree.all-3.5.min.js"></script>
<script src="${js}/bootstrap.min.js"></script>
    
        <div class="modal-dialog" >
            <div class="modal-content">
            
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
                    <h4 class="modal-title">权限修改</h4>
                </div>
                
                <div class="modal-body">
              <ul id="tree" class="ztree" style="width:560px; overflow:auto;"></ul>
                </div>
                
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="btn-save" >保存</button>
                </div>
                
            </div>
        </div>
    
<script type="text/javascript">
var zTree;
var demoIframe;

function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
    var addStr = "<span class='button remove' id='removeBtn_" + treeNode.tId
            + "' title='add node' onfocus='this.blur();'></span>";

    addStr += "<span class='button add' id='addBtn_" + treeNode.tId + "'></span>";
    addStr += "<span class='button edit' id='editBtn_" + treeNode.tId + "'></span>";
    sObj.after(addStr);
    var btn = $("#addBtn_" + treeNode.tId);
    if (btn) btn.bind("click", function () {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.addNodes(treeNode, {id: (1000 + newCount), pId: treeNode.id, name: "new node" + (newCount++)});
        return false;
    });
}

function removeHoverDom(treeId, treeNode) {
    $("#addBtn_" + treeNode.tId).unbind().remove();
    $("#removeBtn_" + treeNode.tId).unbind().remove();
    $("#editBtn_" + treeNode.tId).unbind().remove();
}

var setting = {
    check: {
        enable: true
    },
    view: {
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        dblClickExpand: false,
        showLine: true,
        selectedMulti: false
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: ""
        }
    },
    callback: {
        beforeClick: function (treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("tree");
            if (treeNode.isParent) {
                zTree.expandNode(treeNode);
                return false;
            } else {
                demoIframe.attr("src", treeNode.file + ".html");
                return true;
            }
        }
    }
};

var zNodes = [
    {"id":"101", "pId": "1", "name": "查看", "file": "core/standardData", "perm": "+user+1", "type": "1", "checked": "${fns:setContains(permString, '+user+1')}"},
    {id:102, pId: 1, name: "新建", file: "core/simpleData", perm: "+user+2", type: 1, checked: '${fns:setContains(permString, "+user+2")}'},
    {id:103, pId: 1, name: "修改", file: "core/noline", perm: "+user+4", type: 1, checked: '${fns:setContains(permString, "+user+4")}'},
    {id:104, pId: 1, name: "删除", file: "core/noicon", perm: "+user+8", type: 1, checked: '${fns:setContains(permString, "+user+8")}'},
    {"id":"1", "pId": "0", "name": "用户管理", "open": "true"},
    {id:201, pId: 2, name: "查看", file: "core/standardData", perm: "+device+1", type: 1, checked: '${fns:setContains(permString, "+device+1")}'},
    {id:202, pId: 2, name: "新建", file: "core/simpleData", perm: "+device+2", type: 1, checked: '${fns:setContains(permString, "+device+2")}'},
    {id:203, pId: 2, name: "修改", file: "core/noline", perm: "+device+4", type: 1, checked: '${fns:setContains(permString, "+device+4")}'},
    {id:204, pId: 2, name: "删除", file: "core/noicon", perm: "+device+8", type: 1, checked: '${fns:setContains(permString, "+device+8")}'},
    {id:2, pId: 0, name: "设备管理", open: true},
    {id:301, pId: 3, name: "查看", file: "core/standardData", perm: "+hospital+1", type: 1, checked: '${fns:setContains(permString, "+hospital+1")}'},
    {id:302, pId: 3, name: "新建", file: "core/simpleData", perm: "+hospital+2", type: 1, checked: '${fns:setContains(permString, "+hospital+2")}'},
    {id:303, pId: 3, name: "修改", file: "core/noline", perm: "+hospital+4", type: 1, checked: '${fns:setContains(permString, "+hospital+4")}'},
    {id:304, pId: 3, name: "删除", file: "core/noicon", perm: "+hospital+8", type: 1, checked: '${fns:setContains(permString, "+hospital+8")}'},
    {id:3, pId: 0, name: "医院管理", open: true},
    {id:401, pId: 4, name: "查看", file: "core/standardData", perm: "+baseData+1", type: 1, checked: '${fns:setContains(permString, "+baseData+1")}'},
    {id:402, pId: 4, name: "新建", file: "core/simpleData", perm: "+baseData+2", type: 1, checked: '${fns:setContains(permString, "+baseData+2")}'},
    {id:403, pId: 4, name: "修改", file: "core/noline", perm: "+baseData+4", type: 1, checked: '${fns:setContains(permString, "+baseData+4")}'},
    {"id":"404", "pId": "4", "name": "删除", "file": "core/noicon", "perm": "+baseData+8", "type": "1", "checked": "${fns:setContains(permString, '+baseData+8')}"},
    {id:4, pId: 0, name: "基础数据管理", open: true},
];

var zNodes2 = ${jsonList};

var permString = [
    <c:forEach items="${permString}" var="perm" varStatus="status">
    '${perm}'<c:if test="${not status.last}">,</c:if>
    </c:forEach>
];

$(document).ready(function () {
    var t = $("#tree");
    t = $.fn.zTree.init(t, setting, zNodes2);
    demoIframe = $("#testIframe");
    demoIframe.bind("load", loadReady);
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var $save = $('#btn-save');

    // 勾选已有权限
    $.each(permString, function(index, perm) {
        var node = zTree.getNodesByParam('perm', perm, null);
        if (node != null) {
            zTree.checkNode(node, true, true);
        }
    });

    $save.click(function() {
        var nodes = zTree.getNodesByParam('type', 1, null);
        var perms = [];
        $.each(nodes, function(index, node) {
            if (node.checked) {
                perms.push(node.perm);
            }
        });
        $.ajax({
            url: '${ctx}/admin/mgmt/role/${roleId}',
            data: $.param({"perms":perms}, true),
            dataType: 'json',
            type: 'POST',
            success: function(json, textStatus, jqXHT) {
                if (json.success) {
                    window.location.reload();
                	//iframe1.window.location.reload();
                	//window.parent.$("#dialog-Show").modal('hide');
                }
            }
        });
    });
    
});

function loadReady() {
    var bodyH = demoIframe.contents().find("body").get(0).scrollHeight,
            htmlH = demoIframe.contents().find("html").get(0).scrollHeight,
            maxH = Math.max(bodyH, htmlH), minH = Math.min(bodyH, htmlH),
            h = demoIframe.height() >= maxH ? minH : maxH;
    if (h < 530) h = 530;
    demoIframe.height(h);
}
</script>